package ch.unisi.inf.java.project.riskbami.view;

import ch.unisi.inf.java.project.riskbami.model.*;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Checkbox;

/**
 * Class to handle the popup window that shows the cards
 * 
 * @author minellir@lu.unisi.ch
 * @author babazadm@lu.unisi.ch
 */
public final class ShowCards extends JFrame
{
   
    private final Game game;
    private JLabel cardImageLabel;
    private JPanel cardImagePanel;
    private final int totalCardsOwned;
    private JFrame tradeFrame;
    private final Checkbox[] checkBoxes;
    protected final boolean[] checkBoxesStatus;
    private JButton tradeButton;
    
    /**
     * Constructor for the class ShowCards.
     */
    public ShowCards(final Game game) {
        this.game = game;
        totalCardsOwned = game.getCurrentPlayer().getOwnedCards().size();
        this.checkBoxes = new Checkbox[totalCardsOwned];
        this.checkBoxesStatus = new boolean[totalCardsOwned];
        makeFrame();
    }
    
    /**
     * Creates a frame to trade the cards.
     */
    private final void makeFrame(){
        //Creates the frame for the trade card pop up
        tradeFrame = new JFrame("RiskBaMi - Cards");
        
        //Container stuff
        Container contentPane = tradeFrame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        
        //Panels for the frame
        final JPanel cardPanel = new JPanel(new BorderLayout());
        cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.X_AXIS));
        final JPanel bottomPanel = new JPanel(new BorderLayout());
        contentPane.add(cardPanel);
        contentPane.add(bottomPanel, BorderLayout.SOUTH);
        
        //Works on the panels to display the cards 
        final JPanel cardImagePanel = new JPanel();
        cardImagePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Cards you own"));
        cardPanel.add(cardImagePanel);
        
        //Create the frame for the card
         if(totalCardsOwned < 1) {
             final JLabel noCardLabel = new JLabel("You don't have cards");
             cardImagePanel.add(noCardLabel);
        } else { 
            for(int i = 0; i < totalCardsOwned; i++){
                cardImageLabel = new JLabel(new ImageIcon(getClass().getResource("images/" + game.getCurrentPlayer().getOwnedCards().get(i).getType()  + ".jpg")));
                cardImagePanel.add(cardImageLabel, BorderLayout.CENTER);
                Checkbox selectedCard = new Checkbox("<-- Select");
                checkBoxes[i] = selectedCard;
                cardImagePanel.add(selectedCard, BorderLayout.SOUTH);
            }
        } 
        
        final JPanel buttonPanel = new JPanel();
        bottomPanel.add(buttonPanel, BorderLayout.CENTER);
        tradeButton = new JButton("Trade Cards!");
        
        if(game.getTurnPhase() == 1 && totalCardsOwned >= 3){
            tradeButton.setEnabled(true);
        } else {
            tradeButton.setEnabled(false);
        }
        
        tradeButton.addActionListener(new TradeCardsListener(game, this));
        buttonPanel.add(tradeButton);
         
        tradeFrame.setSize(300, 200);
        tradeFrame.setVisible(true);
        tradeFrame.pack();
        tradeFrame.setResizable(false);
    }
    
    /**
     * Computes the status of the checkboxes (if there were more or less than 3 boxes checked).
     */
    private final int computeStatus(){
        int selectedCards = 0;
        for(int i = 0; i < totalCardsOwned; i++) {
               checkBoxesStatus[i] = checkBoxes[i].getState();
               if(checkBoxesStatus[i] == true) {
                   selectedCards++;
               }
            }
        return selectedCards;
    }
    
    /**
     * Closes the frame.
     */
    private final void close(){
        tradeFrame.setVisible(false);
    }
    
    //Inner classes for the button listener
   
    /**
     * TradeCardsListener listens when the button has been clicked and computes the result of the trading (if there is one).
     */
    private final class TradeCardsListener implements ActionListener{
        private final int totalCardsOwned;
        private final ShowCards tradeFrame;
        
        public TradeCardsListener(final Game game, final ShowCards tradeFrame) {
            this.totalCardsOwned = game.getCurrentPlayer().getOwnedCards().size();
            this.tradeFrame = tradeFrame;
        }
       public final void actionPerformed(final ActionEvent e) {
           final int count = computeStatus();
            //Check if there are more or less than 3 cards selected
            if(count != 3) { 
                JOptionPane.showMessageDialog(tradeFrame,"You have to select exactly 3 cards", "RiskBaMi - Cards trading warning",JOptionPane.WARNING_MESSAGE);
            } else {
                //Create the array list of card
                final ArrayList<Card> tradeCards = new ArrayList<Card>();
                for(int i = 0; i < totalCardsOwned; i++){
                    if(checkBoxesStatus[i] == true){
                        tradeCards.add(game.getCurrentPlayer().getOwnedCards().get(i));
                    }
                }
                UseCardsCommand use = new UseCardsCommand(game, tradeCards);
                if(use.canExecute()){
                    use.execute();
                }
            }
            close();
        }
    }
}
